import java.util.*;

public class Ball {
    static public final int ROW = 15;
    static public final int COL = 60;
    static public char bg[][] = new char[ROW][COL];
    static public int x = 3, y = 3, x_inc = 1, y_inc = 1, color = 37;
    static public Timer t = new Timer();

    static public void p(String format, Object ... obj) {
        System.out.printf(format, obj);
    }

    static public void sleep(int ms) {
        try {
            Thread.sleep(ms);
        }  catch (Exception e) {}
    }

    static public void init(char ch) {
        int i, j;
        for (i = 0; i < ROW; ++i) {
            for (j = 0; j < COL; ++j) {
                if (j == 0 || i == 0 || i == ROW - 1 || j == COL - 1)
                    bg[i][j] = ch;
                else
                    bg[i][j] = ' ';
            }
        }
    }

    static public void draw(char ch) {
        bg[x][y] = ch;
    }

    static public int move() {
        if (x == ROW - 1 || x == 0)
            x_inc *= -1;
        if (y == COL - 1 || y == 0)
            y_inc *= -1;

        x += x_inc;
        y += y_inc;

        return 0;
    }

    static public void show() {
        int i, j;

        Vt.gotoxy(1, 1);
        for (i = 0; i < ROW; ++i) {
            for (j = 0; j < COL; ++j) {
                if (bg[i][j] == '@')
                    Vt.out("".valueOf(bg[i][j]), color, 1);
                else
                    System.out.printf("%c", bg[i][j]);
            }
            System.out.printf("\n");
        }
    }

    static public void ballInit() {
        Vt.cmd(Vt.SCREEN).cmd(Vt.HIDE);
        Kyo.init();
    }

    static public void ballExit() {
        t.cancel();
        Kyo.exit();
        Vt.cmd(Vt.SHOW).gotoxy(ROW + 1, 1);
    }

    static public void main(String [] args) {
        int in;

        ballInit();

        t.schedule(new TimerTask() {
            public void run() {
                init('#');
                draw('@');
                move();
                show();
            }
        }, 100, 100);

        while (true) {
            in = Kyo.key();
            if (in == 27)
                break;
            else if (in == 'w')
                x_inc = -1;
            else if (in == 's')
                x_inc = 1;
            else if (in == 'a')
                y_inc = -1;
            else if (in == 'd')
                y_inc = -1;
            else if (in == 32)
                color = Kyo.rand(30, 38);
        }

        ballExit();
    }
}
